package com.sangcomz.fishbun.util;

import ohos.agp.render.Paint;
import ohos.agp.utils.Rect;

/**
 * Sets the text size for a Paint object so a given string of text will be a
 * given width.
 *
 * @param Paint paint the Paint to set the text size for
 * @param desiredWidth the desired width
 * @param text the desired width
 */
public final class DrawUtil {
    public static final void setTextSizeForWidth(Paint paint, String text, float desiredWidth) {
        float defaultTextSize = 44.0F;
        Rect textBounds = new Rect();
        paint.setTextSize((int) defaultTextSize);
        paint.getTextBounds(text);
        if ((float) textBounds.getWidth() > desiredWidth) {
            float desiredTextSize = defaultTextSize * (desiredWidth / (float) textBounds.getWidth());
            paint.setTextSize((int) desiredTextSize);
        }

    }
}
